package com.longrise.android.byjk.plugins.dealsituation.reversion;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.Course;
import com.longrise.android.byjk.utils.JsonUtil;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.List;


/**
 * Created by sujizhong on 2017/2/26.
 * 我的课程
 */

public class CourseListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "CourseListActivity";

    public static final String COURSE_DETAIL_EXTRA_PARAMS = "COURSE_DETAIL_EXTRA_PARAMS";

    private SwipeRefreshLayout mSwipeLayout;
    private RecyclerView mRecylerView;

    private RelativeLayout mBack;
    private TextView mTitle;

    private boolean mDestory;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {

        }
        return R.layout.activity_courselist;
    }


    @Override
    public void initView() {
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout_courlist);
        setSwipeRefreshLayoutColor();
        mSwipeLayout.setOnRefreshListener(this);
        mRecylerView = (RecyclerView) findViewById(R.id.recyclerview_course);
        setRecyclerViewLayoutManager();
        mBack = (RelativeLayout) findViewById(R.id.back_new);
        mTitle = (TextView) findViewById(R.id.tv_title_new);
        mTitle.setText("我的课程");

        adapter();
        onEvent();

        if (checkNetWorkEnable()) {
            mSwipeLayout.setRefreshing(true);
            request();
        }
    }

    private void setSwipeRefreshLayoutColor() {
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_green_light, android.R.color.holo_blue_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
    }

    private void setRecyclerViewLayoutManager() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecylerView.setLayoutManager(layoutManager);
    }

    private void onEvent() {
        if (mBack != null) {
            mBack.setOnClickListener(mListener);
        }
    }

    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.back_new) {
                close();
            }
        }
    };

    private void adapter() {
//        mAdapter = new CourseListAdapter(this) {
//
//            @Override
//            public void onItemClickListener(int position, Course course) {
//                openCourseDetailActivity(course);
//            }
//        };
//        mRecylerView.setAdapter(mAdapter);
    }

    private void openCourseDetailActivity(Course course){
//        Intent intent = new Intent(this, CourseDetailActivity.class);
//        intent.putExtra(COURSE_DETAIL_EXTRA_PARAMS, course);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void request() {
        EntityBean bean = new EntityBean();
        EntityBean reqBean = new EntityBean();
        reqBean.set("bean", bean);
        commit(reqBean, "bbt_course_sMyCourseBeans");
    }

    private void commit(EntityBean bean, String api) {
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, api, bean, mRequestCallListener);
    }

    private LoadDataManager.OnRequestCallListener mRequestCallListener = new LoadDataManager.OnRequestCallListener() {

        @Override
        public void onSuccess(String key, String service, Object result) {
            try {
                if (mDestory) return;
                EntityBean resultBean = (EntityBean) result;
                if (resultBean != null) {
                    String resultStr = resultBean.getString("result");
                    parse(resultStr);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
            if (mDestory) return;
            showToast(getString(R.string.nonetwork));
        }

        @Override
        public void onFinished(String key, String service) {
            if (mSwipeLayout != null) {
                mSwipeLayout.setRefreshing(false);
            }
        }
    };

    private void parse(String resultStr) throws Exception {
        List<Course> courses = JsonUtil.fromJson(resultStr, new TypeToken<List<Course>>() {
        }.getType());
//        if (mAdapter != null) {
//            mAdapter.clear();
//            mAdapter.addAll(courses);
//        }
    }

    private void close() {
        finish();
    }

    @Override
    public void onRefresh() {
        if (checkNetWorkEnable()) {
            request();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDestory = true;
    }
}
